//
//  StatusCell.swift
//  新浪微博01
//
//  Created by 李国伟 on 15/12/11.
//  Copyright © 2015年 李国伟. All rights reserved.
//

import UIKit
/// 微博 Cell 中控件的间距数值
let StatusCellMargin: CGFloat = 12
/// 微博头像的宽度

let StatusCellIconWidth: CGFloat = 35

class StatusCell: UITableViewCell {

    var viewModel: StatusViewModel?{
        didSet{
            topView.viewModel = viewModel
            contentLabel.text = viewModel?.status.text
            pictureView.viewModel = viewModel
            pictureView.snp_updateConstraints{ (make) -> Void in
                make.height.equalTo(pictureView.bounds.height)
                // 直接设置宽度数值
                make.width.equalTo(pictureView.bounds.width)
                //消除无配图多余间距移动到了子类
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
       self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func rowHeight(vm: StatusViewModel) -> CGFloat{
        
        viewModel = vm
        layoutIfNeeded()
        return CGRectGetMaxY(bottomView.frame)
    }
    
    //懒加载控件
     lazy var topView: StatusCellTopView = StatusCellTopView()
     lazy var contentLabel: UILabel = UILabel(title: "微博正文", fontsize: 15, screenInset: StatusCellMargin, color: UIColor.darkGrayColor())
     lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
     lazy var pictureView: StatusPictureView = StatusPictureView()
}

//设置控件，添加约束
extension StatusCell{
    
     func setupUI() {
    
        selectionStyle = .None
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(bottomView)
        contentView.addSubview(pictureView)
        bottomView.backgroundColor = UIColor(white: 0.9, alpha: 1)
        pictureView.backgroundColor = UIColor(white: 0.9, alpha: 1)
        
        // 2. 自动布局
        // 1> 顶部视图
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * StatusCellMargin + StatusCellIconWidth)
        }
        // 2> 内容标签
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
        }
        // 3> cell配图,移动到子类中
        //4底部视图
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView)
            make.right.equalTo(contentView)
            make.height.equalTo(44)
            
            // 指定向下的约束,如果是动态计算必须去掉
            // make.bottom.equalTo(contentView.snp_bottom)
        }
    }
}